/**
 * 
 */
package org.vsg.common.pagination.apps;

import java.util.Collection;
import java.util.Vector;

import org.vsg.common.pagination.AdvancedPagination;
import org.vsg.common.pagination.GoToPageDefinedException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationConstant;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.PaginationParseRequestException;
import org.vsg.common.pagination.carrier.AdvancedArrayListValueCarrier;
import org.vsg.common.pagination.carrier.ArrayListValueCarrier;
import org.vsg.common.pagination.carrier.CarrierNotSupportedException;
import org.vsg.common.pagination.carrier.CollectionCarrier;
import org.vsg.common.pagination.carrier.GroupingValueCarrier;
import org.vsg.common.pagination.carrier.ValueCarrier;
import org.vsg.common.pagination.wrapper.AdvancedArrayListPaginationWrapper;
import org.vsg.common.pagination.wrapper.ArrayListPaginationWrapper;
import org.vsg.common.pagination.wrapper.PaginationWrapper;
import org.vsg.common.pagination.wrapper.PaginationWrapperException;

/**
 * @author Bill Vison
 *
 */
public class CollectionPaginationApplication extends AbstractPaginationApplication {


	private Collection coll = new Vector();
	
	private int groupingSize = PaginationConstant.DEFAULT_GROUPINGSIZE;
	
	private int pageIndex;
	
	private int groupingIndex;
	
	
	private byte indexDefined; // '0' is not defined , '1' pageIndex defined , '2' groupingIndex defined
	
	/**
	 * @deprecated
	 * @param collection
	 * @param gotopage
	 * @return
	 * @throws PaginationParseRequestException
	 */
	public ArrayListValueCarrier newArrayParameberVO(Collection collection , int gotopage) throws PaginationParseRequestException{
		ArrayListValueCarrier valueObj = new ArrayListValueCarrier();
		valueObj.setDataList(collection);
		try {
			valueObj.setPageIndex(gotopage);
		} catch (PaginationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		valueObj.setPageSize(pageSize);
		return valueObj;
	}
	
	
	public void gotoPage(int gotopage) throws GoToPageDefinedException{
		if (indexDefined == (byte)2) {
			throw new GoToPageDefinedException("Grouping Index had been defined! ");
		}
		this.pageIndex = gotopage;
		this.indexDefined = (byte)1;
		
	}
	
	public void gotoGrouping(int gotoGrouping) throws GoToPageDefinedException{
		if (indexDefined == (byte)1) {
			throw new GoToPageDefinedException("Page Index had been defined! ");
		}
		this.groupingIndex = gotoGrouping;
		this.indexDefined = (byte)2;
	}
	
	public PaginationWrapper generatePaginatonWrapper(Pagination page) throws PaginationWrapperException {
		PaginationWrapper pageWrapper = null;
		
		if (page instanceof AdvancedPagination) {
			pageWrapper = new AdvancedArrayListPaginationWrapper();			
		}
		else {
			pageWrapper = new ArrayListPaginationWrapper();
		}
		pageWrapper.loadPagination(page);
		return pageWrapper;
	}
	
	public void setDataList(Collection coll) {
		this.coll = coll;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.apps.PaginationApplication#initializeConfiguration()
	 */
	public void initializeConfiguration() throws PaginationApplicationContextException {
		// TODO Auto-generated method stub
		
		// get page size 
		String envPageSize = this.getConfiguration().getProperty(EnvironmentProperty.PAGE_SIZE);
		if (envPageSize != null) {
			try {
				pageSize = Integer.parseInt(envPageSize);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				logger.warn("Get the pageSize from Environment Properties : " + pageSize);
				e.printStackTrace();
			}
		}
		
		String envGroupingSize = this.getConfiguration().getProperty(EnvironmentProperty.GROUPING_SIZE);
		if (envGroupingSize != null) {
			try {
				groupingSize = Integer.parseInt(envGroupingSize);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				logger.warn("Get the groupingSize from Environment Properties : " + groupingSize);
				e.printStackTrace();
			}
			
		}
	}

	
	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.apps.PaginationApplication#generateValueCarrier(java.lang.Class)
	 */
	public ValueCarrier generateValueCarrier(Class cls) throws CarrierNotSupportedException,InstantiationException ,IllegalAccessException,PaginationException{
		// TODO Auto-generated method stub
		Object obj = cls.newInstance();
		
		// load all collection
		if (!(obj instanceof CollectionCarrier)) {
			throw new CarrierNotSupportedException("Class "+cls+" is not supported to CollectionValueCarrier Interface! ");
		} else {
			CollectionCarrier cvc = (CollectionCarrier)obj;
			cvc.setDataList(this.coll);
			
			cvc = null;
		}
		
		if (obj instanceof ValueCarrier) {
			ValueCarrier vc = (ValueCarrier)obj;
			vc.setPageSize(pageSize);
			if (this.indexDefined == (byte)1) {
				vc.setPageIndex(pageIndex);
			}
		}
		
		if (obj instanceof GroupingValueCarrier) {

			GroupingValueCarrier avc = (GroupingValueCarrier)obj;
			avc.setGroupingSize(this.groupingSize);
			
			if (this.indexDefined == (byte)2) {
				int gotopage = this.groupingIndex * groupingSize; 
				avc.setPageIndex(gotopage);				
			}

		}


		return (ValueCarrier)obj;
	}
	
}
